﻿<r:RibbonWindow x:Class="CheckbookManager.CheckManager"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:me="clr-namespace:CheckbookManager"
    xmlns:Data="clr-namespace:CheckbookManager.Data"
    xmlns:Converters="clr-namespace:CheckbookManager.Converters"
    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
    Height="600" Width="800"  WindowStartupLocation="CenterScreen"
    FontFamily="Arial" Title="Personal Finance Manager | Checking Account" Background="{DynamicResource WindowBackground}">

    <!-- Command bindings -->
    <Window.CommandBindings>
        <CommandBinding Command="me:AppCommands.AddNew" Executed="OnAddNewEntry" />
        <CommandBinding Command="me:AppCommands.Clear" Executed="OnClearEntry" CanExecute="OnHasSelectedEntries" />
        <CommandBinding Command="me:AppCommands.Delete" Executed="OnDeleteEntry" CanExecute="OnHasSelectedEntries" />
        <CommandBinding Command="me:AppCommands.Help" Executed="OnIgnore" />
        <CommandBinding Command="me:AppCommands.Reconcile" Executed="OnIgnore" />
        <CommandBinding Command="me:AppCommands.CashflowReport" Executed="OnIgnore" />
        <CommandBinding Command="me:AppCommands.BudgetReport" Executed="OnIgnore" />
        <CommandBinding Command="me:AppCommands.Transfer" Executed="OnIgnore" />
        <CommandBinding Command="me:AppCommands.DownloadStatements" Executed="OnIgnore" />
        <CommandBinding Command="me:AppCommands.DownloadCreditCards" Executed="OnIgnore" />
        <CommandBinding Command="me:AppCommands.Backup" Executed="OnIgnore" />
        <CommandBinding Command="me:AppCommands.Calculator" Executed="OnIgnore" />
    </Window.CommandBindings>

    <!-- Dock used for primary layout -->
    <DockPanel>
        <DockPanel.Resources>

            <!-- Converters -->
            <Converters:AmountToCreditDebitConverter x:Key="amountConverter" />
            <Converters:PositiveNumberToColorConverter x:Key="BalanceDisplayConverter" PositiveColor="Black" NegativeColor="Red" />

            <!-- Brushes -->
            <LinearGradientBrush x:Key="dgHeaderBrush" StartPoint="0.5,0" EndPoint="0.5,1">
                <GradientStop Color="#FF6B8E95" Offset="0"/>
                <GradientStop Color="#FF14A7C1" Offset="1"/>
                <GradientStop Color="#FF1E424E" Offset="0.509"/>
                <GradientStop Color="#FF1D4855" Offset="0.542"/>
                <GradientStop Color="#FF1D4855" Offset="0.542"/>
                <GradientStop Color="#FF193A44" Offset="0.526"/>
            </LinearGradientBrush>

            <LinearGradientBrush x:Key="dgHeaderBorderBrush" StartPoint="0.5,0" EndPoint="0.5,1">
                <GradientStop Color="#FF1D1D1D" Offset="0.614"/>
                <GradientStop Color="#FF007F96" Offset="0.853"/>
                <GradientStop Color="#FF0AEAFA" Offset="1"/>
            </LinearGradientBrush>

            <Style x:Key="dgHeaderStyle" TargetType="dg:DataGridColumnHeader">
                <Setter Property="Background" Value="{StaticResource dgHeaderBrush}" />
                <Setter Property="Foreground" Value="White" />
                <Setter Property="BorderBrush" Value="{StaticResource dgHeaderBorderBrush}" />
                <Setter Property="BorderThickness" Value="1" />
                <Setter Property="SnapsToDevicePixels" Value="True" />
                <Setter Property="HorizontalContentAlignment" Value="Center" />
                <Setter Property="MinWidth" Value="0" />
                <Setter Property="MinHeight" Value="30" />
                <Setter Property="Cursor" Value="Hand" />
            </Style>

            <Style x:Key="dgCellStyle" TargetType="dg:DataGridCell"
                   BasedOn="{StaticResource {x:Type dg:DataGridCell}}">
                <Setter Property="SnapsToDevicePixels" Value="True" />
                <Setter Property="VerticalAlignment" Value="Center" />
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="Transparent" />
                        <Setter Property="BorderBrush" Value="Transparent" />
                        <Setter Property="Foreground" Value="Black" />
                    </Trigger>
                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                        <Setter Property="Background" Value="{StaticResource whiteBackBrush}" />
                        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static dg:DataGrid.FocusBorderBrushKey}}" />
                        <Setter Property="Foreground" Value="Black" />
                    </Trigger>
                </Style.Triggers>
            </Style>

            <Style x:Key="dgRowStyle" TargetType="dg:DataGridRow">
                <Setter Property="SnapsToDevicePixels" Value="True" />
                <Setter Property="Background" Value="White" />
                <Style.Triggers>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter Property="Background" Value="#FFD0D0E0" />
                    </Trigger>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="LightGoldenrodYellow" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </DockPanel.Resources>

        <r:Ribbon DockPanel.Dock="Top" Title="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}},Path=Title}">

            <r:Ribbon.Resources>
                <r:RibbonGroupSizeDefinitionCollection x:Key="RibbonLayout">
                    <r:RibbonGroupSizeDefinition>
                        <!-- Control sizes: L,L,L -->
                        <r:RibbonControlSizeDefinition ImageSize="Large" IsLabelVisible="True"/>
                        <r:RibbonControlSizeDefinition ImageSize="Large" IsLabelVisible="True"/>
                        <r:RibbonControlSizeDefinition ImageSize="Large" IsLabelVisible="True"/>
                    </r:RibbonGroupSizeDefinition>
                    <r:RibbonGroupSizeDefinition>
                        <!-- Control sizes: L,M,M -->
                        <r:RibbonControlSizeDefinition ImageSize="Large" IsLabelVisible="True"/>
                        <r:RibbonControlSizeDefinition ImageSize="Small" IsLabelVisible="True"/>
                        <r:RibbonControlSizeDefinition ImageSize="Small" IsLabelVisible="True"/>
                    </r:RibbonGroupSizeDefinition>
                    <r:RibbonGroupSizeDefinition>
                        <!-- Control sizes: L,S,S -->
                        <r:RibbonControlSizeDefinition ImageSize="Large" IsLabelVisible="True"/>
                        <r:RibbonControlSizeDefinition ImageSize="Small" IsLabelVisible="False"/>
                        <r:RibbonControlSizeDefinition ImageSize="Small" IsLabelVisible="False"/>
                    </r:RibbonGroupSizeDefinition>
                    <!-- Collapsed -->
                    <r:RibbonGroupSizeDefinition IsCollapsed="True" />
                </r:RibbonGroupSizeDefinitionCollection>
            </r:Ribbon.Resources>

            <!-- Define the application icon in the org -->
            <r:Ribbon.ApplicationMenu>
                <r:RibbonApplicationMenu>
                    <r:RibbonApplicationMenu.Command>
                        <r:RibbonCommand 
                            Executed="OnCloseApplication"
                            LabelTitle="Application Button"
                            LabelDescription="Close the application."
                            SmallImageSource="images/Coins.png"
                            LargeImageSource="images/Coins.png"
                            ToolTipTitle="Personal Finance Manager"
                            ToolTipDescription="Click here to open or save a checkbook register." />
                    </r:RibbonApplicationMenu.Command>
                    <r:RibbonApplicationMenuItem>
                        <r:RibbonApplicationMenuItem.Command>
                            <r:RibbonCommand 
                                LabelTitle="_Close" 
                                LabelDescription="Close the Application"
                                Executed="OnCloseApplication" />
                        </r:RibbonApplicationMenuItem.Command>
                    </r:RibbonApplicationMenuItem>
                </r:RibbonApplicationMenu>
            </r:Ribbon.ApplicationMenu>

            <!-- Quick pick menu -->
            <r:Ribbon.QuickAccessToolBar>
                <r:RibbonQuickAccessToolBar CanUserCustomize="True">
                    <r:RibbonButton Command="me:AppCommands.AddNew" r:RibbonQuickAccessToolBar.Placement="InCustomizeMenuAndToolBar" />
                    <r:RibbonButton Command="me:AppCommands.Cut" r:RibbonQuickAccessToolBar.Placement="InCustomizeMenuAndToolBar" />
                    <r:RibbonButton Command="me:AppCommands.Copy" r:RibbonQuickAccessToolBar.Placement="InCustomizeMenuAndToolBar" />
                    <r:RibbonButton Command="me:AppCommands.Paste"  r:RibbonQuickAccessToolBar.Placement="InCustomizeMenuAndToolBar" />
                    <r:RibbonButton Command="me:AppCommands.Help" r:RibbonQuickAccessToolBar.Placement="InToolBar" />
                </r:RibbonQuickAccessToolBar>
            </r:Ribbon.QuickAccessToolBar>

            <r:RibbonTab Label="Banking">

                <!-- Define the groups in this tab -->
                <r:RibbonTab.Groups>

                    <!-- Clipboard commands -->
                    <r:RibbonGroup GroupSizeDefinitions="{StaticResource RibbonLayout}">
                        <r:RibbonGroup.Command>
                            <r:RibbonCommand LabelTitle="Clipboard" SmallImageSource="images/Paste.png" />
                        </r:RibbonGroup.Command>
                        <r:RibbonButton Command="me:AppCommands.Cut"/>
                        <r:RibbonButton Command="me:AppCommands.Copy"/>
                        <r:RibbonButton Command="me:AppCommands.Paste"/>
                    </r:RibbonGroup>

                    <r:RibbonGroup GroupSizeDefinitions="{StaticResource RibbonLayout}">
                        <r:RibbonGroup.Command>
                            <r:RibbonCommand LabelTitle="Checkbook" SmallImageSource="images/AddNew.png" />
                        </r:RibbonGroup.Command>
                        <r:RibbonButton Command="me:AppCommands.AddNew"/>
                        <r:RibbonButton Command="me:AppCommands.Clear" />
                        <r:RibbonButton Command="me:AppCommands.Delete"/>
                    </r:RibbonGroup>

                    <r:RibbonGroup GroupSizeDefinitions="{StaticResource RibbonLayout}">
                        <r:RibbonGroup.Command>
                            <r:RibbonCommand LabelTitle="Statements" SmallImageSource="images/Reconcile.png" />
                        </r:RibbonGroup.Command>
                        <r:RibbonButton Command="me:AppCommands.Reconcile"/>
                    </r:RibbonGroup>

                    <r:RibbonGroup GroupSizeDefinitions="{StaticResource RibbonLayout}">
                        <r:RibbonGroup.Command>
                            <r:RibbonCommand LabelTitle="Online" SmallImageSource="images/CreditCards.png" />
                        </r:RibbonGroup.Command>

                        <r:RibbonButton Command="me:AppCommands.DownloadStatements"/>
                        <r:RibbonButton Command="me:AppCommands.DownloadCreditCards"/>
                        <r:RibbonButton Command="me:AppCommands.Transfer"/>
                    </r:RibbonGroup>

                    <r:RibbonGroup GroupSizeDefinitions="{StaticResource RibbonLayout}">
                        <r:RibbonGroup.Command>
                            <r:RibbonCommand LabelTitle="Tools" SmallImageSource="images/Backup.png" />
                        </r:RibbonGroup.Command>

                        <r:RibbonButton Command="me:AppCommands.Backup"/>
                        <r:RibbonButton Command="me:AppCommands.Calculator"/>
                    </r:RibbonGroup>

                </r:RibbonTab.Groups>
            </r:RibbonTab>

            <r:RibbonTab Label="Reporting">
                <r:RibbonTab.Groups>
                    <r:RibbonGroup GroupSizeDefinitions="{StaticResource RibbonLayout}">
                        <r:RibbonGroup.Command>
                            <r:RibbonCommand LabelTitle="Reports" SmallImageSource="images/CashflowReport.png" />
                        </r:RibbonGroup.Command>

                        <r:RibbonButton Command="me:AppCommands.TrendReport" />
                        <r:RibbonButton Command="me:AppCommands.BudgetReport"/>
                        <r:RibbonDropDownButton Command="me:AppCommands.OtherReports">
                            <MenuItem Header="Cash Flow Report" />
                            <MenuItem Header="Favorites Report" />
                            <MenuItem Header="Spending Report" />
                            <MenuItem Header="Savings Report" />
                            <MenuItem Header="Credit Report" />
                        </r:RibbonDropDownButton>

                    </r:RibbonGroup>
                </r:RibbonTab.Groups>
            </r:RibbonTab>

        </r:Ribbon>

        <!-- Status bar on bottom: holds balance -->
        <StatusBar DockPanel.Dock="Bottom" Background="{StaticResource whiteBackBrush}" 
                   BorderBrush="SteelBlue" BorderThickness="0,1,0,0">
            <StatusBarItem DockPanel.Dock="Right" HorizontalAlignment="Right">
                <StackPanel Margin="5" Orientation="Horizontal">
                    <TextBlock Margin="5,0"  Text="Current Balance:" FontWeight="Bold" />
                    <TextBlock Foreground="{Binding CurrentBalance,Converter={StaticResource BalanceDisplayConverter}}" Text="{Binding CurrentBalance, StringFormat=C}" />
                </StackPanel>
            </StatusBarItem>
        </StatusBar>

        <!-- DataGrid fills remainder of space -->
        <Grid>

            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <dg:DataGrid x:Name="dg" ItemsSource="{Binding}" Margin="10"
                  AutoGenerateColumns="False"
                  Background="#80909090" AlternationCount="2"
                  ColumnHeaderStyle="{StaticResource dgHeaderStyle}"
                  RowStyle="{StaticResource dgRowStyle}"
                  CellStyle="{StaticResource dgCellStyle}"
                  SelectionMode="Extended"
                  SelectionUnit="FullRow"
                  GridLinesVisibility="All"
                  VerticalGridLinesBrush="DarkGray"
                  RowDetailsVisibilityMode="VisibleWhenSelected">

                <dg:DataGrid.RowDetailsTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="20,0,0,0">
                            <TextBlock Text="Category:" VerticalAlignment="Center" FontWeight="Bold" />
                            <TextBox Text="{Binding Category}" Margin="10,5" MinWidth="100">
                                <TextBox.Style>
                                    <Style TargetType="TextBox">
                                        <Setter Property="BorderBrush" Value="{x:Null}" />
                                        <Setter Property="Background" Value="{x:Null}" />
                                        <Style.Triggers>
                                            <Trigger Property="IsFocused" Value="True">
                                                <Setter Property="BorderBrush" Value="{x:Static SystemColors.WindowFrameBrush}" />
                                                <Setter Property="Background" Value="{x:Static SystemColors.WindowBrush}" />
                                            </Trigger>
                                            <Trigger Property="IsMouseOver" Value="True">
                                                <Setter Property="BorderBrush" Value="{x:Static SystemColors.WindowFrameBrush}" />
                                                <Setter Property="Background" Value="{x:Static SystemColors.WindowBrush}" />
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBox.Style>
                            </TextBox>
                        </StackPanel>
                    </DataTemplate>
                </dg:DataGrid.RowDetailsTemplate>

                <dg:DataGrid.ContextMenu >
                    <ContextMenu >
                        <MenuItem Header="Copy Selected Transactions" Command="{x:Static ApplicationCommands.Copy}" />
                    </ContextMenu>
                </dg:DataGrid.ContextMenu>

                <!-- Column definition -->
                <dg:DataGrid.Columns>
                    <dg:DataGridTextColumn Header="No." Width="SizeToCells"  Binding="{Binding CheckNumber}" />

                    <dg:DataGridTemplateColumn Header="Date" MinWidth="100">
                        <dg:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <dg:DatePicker SelectedDate="{Binding Date}" SelectedDateFormat="Short" />
                            </DataTemplate>
                        </dg:DataGridTemplateColumn.CellEditingTemplate>
                        <dg:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Date, StringFormat=d}" />
                            </DataTemplate>
                        </dg:DataGridTemplateColumn.CellTemplate>
                    </dg:DataGridTemplateColumn>

                    <dg:DataGridTextColumn Header="Pay To" MinWidth="200" Binding="{Binding Recipient}">
                        <dg:DataGridTextColumn.CellStyle>
                            <Style TargetType="dg:DataGridCell" BasedOn="{StaticResource dgCellStyle}">
                                <Setter Property="FontWeight" Value="Bold" />
                                <Setter Property="Foreground" Value="DarkBlue" />
                            </Style>
                        </dg:DataGridTextColumn.CellStyle>
                    </dg:DataGridTextColumn>

                    <dg:DataGridComboBoxColumn Header="Memo" Width="*"
                           ItemsSource="{Binding Source={x:Static Data:CheckBook.Descriptions}, Mode=OneWay}"
                           TextBinding="{Binding Memo}">
                        <dg:DataGridComboBoxColumn.EditingElementStyle>
                            <Style TargetType="ComboBox">
                                <Setter Property="IsEditable" Value="True" />
                            </Style>
                        </dg:DataGridComboBoxColumn.EditingElementStyle>
                    </dg:DataGridComboBoxColumn>

                    <dg:DataGridCheckBoxColumn Header="C" Width="SizeToHeader" Binding="{Binding Cleared}" ElementStyle="{DynamicResource NoBorderCheckBoxStyle}" EditingElementStyle="{DynamicResource NoBorderCheckBoxStyle}" />
                    <dg:DataGridTextColumn Width="SizeToCells" Header="Payment" Binding="{Binding Amount, Converter={StaticResource amountConverter}, ConverterParameter=0, StringFormat=C}" />
                    <dg:DataGridTextColumn Width="SizeToCells" Header="Deposit" Binding="{Binding Amount, Converter={StaticResource amountConverter}, ConverterParameter=1, StringFormat=C}" />

                    <dg:DataGridTemplateColumn Width="SizeToCells" Header="Balance" ClipboardContentBinding="{Binding TotalBalance}">
                        <dg:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Foreground="{Binding TotalBalance,Converter={StaticResource BalanceDisplayConverter}}" Text="{Binding TotalBalance, StringFormat=C}" />
                            </DataTemplate>
                        </dg:DataGridTemplateColumn.CellTemplate>
                    </dg:DataGridTemplateColumn>

                </dg:DataGrid.Columns>
            </dg:DataGrid>

            <Grid Grid.Row="1">
                <ToggleButton HorizontalAlignment="Right" VerticalAlignment="Top"
                    Click="OnShowGraph"
                    Style="{StaticResource toggleBlueButtonStyle}" Content="Show Graph" />
                
                <me:AccountBalanceGraph x:Name="graph"
                    HorizontalAlignment="Center" /> 
                
            </Grid>

        </Grid>


    </DockPanel>
 </r:RibbonWindow>